<?php
/**
 * 版本控制
 * Created by PhpStorm.
 * User: Yui
 * Date: 2017/5/13 0013
 * Time: 15:43
 */

namespace backend\controllers;

use Yii;
use common\controllers\BackendCommonCtrl;
use common\services\VersionSve;
use yii\helpers\Url;

class VersionController extends BackendCommonCtrl
{
    /**
     * 版本列表
     */
    public function actionList()
    {
        $VersionSve = new VersionSve();
        $rs = $VersionSve->getList([], 'version_id DESC', true);
        return $this->render('list', $rs['data']);
    }

    /**
     * 添加版本
     */
    public function actionAdd()
    {
        $request = Yii::$app->request;
        if ($request->isPost) {
            $VersionSve = new VersionSve();
            $rs = $VersionSve->add($request->post());
            if ($rs['code'] === 0) {
                $rs['redirect'] = Url::toRoute('/version/list');
            }
            return $this->asJson($rs);
        } else {
            return $this->render('add');
        }
    }

    /**
     * 查看
     * @return string
     */
    public function actionView($version_id)
    {
        //实例化版本模型
        $VersionSve = new VersionSve();
        $rs = $VersionSve->getInfo(['version_id' => $version_id]);
        if ($rs['code'] !== 0) {
            #TODO 404
        }
        return $this->render('view', ['Version' => $rs['data']['Obj']]);
    }
}